package controller

import (
	"device-admin/internal/app/web/service/material"
	"device-admin/internal/define"
	"device-admin/internal/pkg/xunfeiyun"
	"fmt"

	"192.168.1.75/go-pkg/helper"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Material struct{}

// 新增素材
func (Material) Add(c *gin.Context) {
	var params struct {
		Type     int    `json:"type"`
		Name     string `json:"name"`
		Content  string `json:"content"`
		FilePath string `json:"file_path"`
		FolderID string `json:"folder_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)

	UserID := c.GetString("user_id")
	Username := c.GetString("username")
	MerchantID := c.GetString("merchant_id")
	err := material.Material{}.Add(c.Request.Context(), MerchantID, UserID, Username, params.Type, params.Name, params.Content, params.FilePath, params.FolderID)
	response.JSON(c, nil, err)
}

// 素材列表
func (Material) List(c *gin.Context) {
	var params struct {
		Type     int    `json:"type"`
		Keywords string `json:"keywords"`
		FolderID string `json:"folder_id"`
		Page     int    `json:"page"`
		Limit    int    `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	UserID := c.GetString("user_id")
	Username := c.GetString("username")
	MerchantID := c.GetString("merchant_id")
	list, total, err := material.Material{}.List(c.Request.Context(), MerchantID, UserID, Username, params.Keywords, params.Type, 0, params.FolderID, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// 可用素材列表
func (Material) UsableList(c *gin.Context) {
	var params struct {
		Type     int    `json:"type"`
		Keywords string `json:"keywords"`
		FolderID string `json:"folder_id"`
		Page     int    `json:"page"`
		Limit    int    `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	UserID := c.GetString("user_id")
	Username := c.GetString("username")
	MerchantID := c.GetString("merchant_id")
	list, total, err := material.Material{}.List(c.Request.Context(), MerchantID, UserID, Username, params.Keywords, params.Type, 1, params.FolderID, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// 试听
func (Material) Sound(c *gin.Context) {
	var params struct {
		Content     string `json:"content"`
		SoundSource string `json:"sound_source"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	// 设置响应头
	filename, _ := helper.UUID{}.String()
	c.Header("Content-Disposition", fmt.Sprintf("inline; filename=%s", filename+".wav")) //inline 直接打开 attachment 强制下载
	c.Header("Content-Type", "audio/wav")
	content := xunfeiyun.XunFeiYun{}.SpeechSynthesis(params.Content, params.SoundSource)
	c.Writer.Write(content)
}

// 启用禁用
func (Material) ChangeStatus(c *gin.Context) {
	var params struct {
		ID     string `json:"id"`
		Status int    `json:"status"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := material.Material{}.ChangeStatus(c.Request.Context(), MerchantID, params.ID, params.Status)
	response.JSON(c, nil, err)
}

// 素材移动分组
func (Material) ChangeFolder(c *gin.Context) {
	var params struct {
		ID       string `json:"id"`
		FolderID string `json:"folder_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := material.Material{}.ChangeFolder(c.Request.Context(), MerchantID, params.ID, params.FolderID)
	response.JSON(c, nil, err)
}

// 素材审核列表
func (Material) AuditList(c *gin.Context) {
	var params struct {
		Page  int `json:"page"`
		Limit int `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	UserID := c.GetString("user_id")
	MerchantID := c.GetString("merchant_id")
	list, total, err := material.Material{}.AuditList(c.Request.Context(), MerchantID, UserID, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// 素材审核
func (Material) Audit(c *gin.Context) {
	var params struct {
		ID     string `json:"id"`
		Status int    `json:"status"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	UserID := c.GetString("user_id")
	Username := c.GetString("username")
	MerchantID := c.GetString("merchant_id")
	err := material.Material{}.Audit(c.Request.Context(), MerchantID, UserID, Username, params.ID, params.Status)
	response.JSON(c, nil, err)
}

// 素材审核记录
func (Material) AuditLog(c *gin.Context) {
	var params struct {
		Page  int `json:"page"`
		Limit int `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	UserID := c.GetString("user_id")
	list, total, err := material.Material{}.AuditLog(c.Request.Context(), MerchantID, UserID, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// 新增身份
func (Material) ThemeSave(c *gin.Context) {
	var params struct {
		ID                string                   `json:"id"`
		Name              string                   `json:"name"`
		SoundSource       string                   `json:"sound_source"`
		Permanent         int                      `json:"permanent"`
		StartTime         int64                    `json:"start_time"`
		EndTime           int64                    `json:"end_time"`
		PersonalityPrompt string                   `json:"personality_prompt"`
		Configs           []material.ConfigsStruct `json:"configs"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	userName := c.GetString("username")
	err := material.MaterialTheme{}.ThemeSave(c.Request.Context(), MerchantID, userName, params.ID, params.Name,
		params.SoundSource, params.Permanent, params.StartTime, params.EndTime, params.PersonalityPrompt, params.Configs)
	response.JSON(c, nil, err)
}

// 绑定设备
func (Material) BindDevice(c *gin.Context) {
	var params struct {
		ID           string `json:"id"`
		BelongDevice string `json:"belong_device"` //robotID,逗号分隔多个
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := material.MaterialTheme{}.BindDevice(c.Request.Context(), MerchantID, params.ID, params.BelongDevice)
	response.JSON(c, nil, err)
}

// 身份列表
func (Material) ThemeList(c *gin.Context) {
	var params struct {
		Name  string `json:"name"`
		Page  int    `json:"page"`
		Limit int    `json:"limit"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	list, total, err := material.MaterialTheme{}.ThemeList(c.Request.Context(), MerchantID, params.Name, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// 身份详情
func (Material) ThemeDetail(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	data, err := material.MaterialTheme{}.ThemeDetail(c.Request.Context(), MerchantID, params.ID)
	response.JSON(c, data, err)
}

// 删除身份
func (Material) ThemeDelete(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := material.MaterialTheme{}.ThemeDelete(c.Request.Context(), MerchantID, params.ID)
	response.JSON(c, nil, err)
}

// 获取默认身份
func (Material) DefaultThemeConfig(c *gin.Context) {
	data, err := material.MaterialTheme{}.DefaultThemeConfig(c.Request.Context())
	response.JSON(c, data, err)
}

// 触发事件列表
func (Material) ConditionOptions(c *gin.Context) {
	data := define.MaterialCondition
	response.JSON(c, data, nil)
}

// 素材分组新增
func (Material) FolderAdd(c *gin.Context) {
	var params struct {
		Name string `json:"name"`
		Type int8   `json:"type"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := material.MaterialFolder{}.Save(c.Request.Context(), MerchantID, "", params.Name, params.Type)
	response.JSON(c, nil, err)
}

// 素材分组编辑
func (Material) FolderSave(c *gin.Context) {
	var params struct {
		ID   string `json:"id"`
		Name string `json:"name"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := material.MaterialFolder{}.Save(c.Request.Context(), MerchantID, params.ID, params.Name, 0)
	response.JSON(c, nil, err)
}

// 素材分组列表
func (Material) FolderList(c *gin.Context) {
	var params struct {
		Type int8 `json:"type"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	list, total, err := material.MaterialFolder{}.List(c.Request.Context(), MerchantID, params.Type)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// 素材分组排序
func (Material) FolderSort(c *gin.Context) {
	var params struct {
		Type int8 `json:"type"`
		Sort []struct {
			ID   string `json:"id"`
			Sort int8   `json:"sort"`
		} `json:"sort"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := material.MaterialFolder{}.Sort(c.Request.Context(), MerchantID, params.Type, params.Sort)
	response.JSON(c, nil, err)
}

// 素材分组删除
func (Material) FolderDelete(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	MerchantID := c.GetString("merchant_id")
	err := material.MaterialFolder{}.Delete(c.Request.Context(), MerchantID, params.ID)
	response.JSON(c, nil, err)
}
